//
//  ST_BTCustomEmojiSegmentControl.swift
//  btvideo
//
//  Created by mac on 2020/7/15.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_BTCustomEmojiSegmentControl: UIView {
    
    static let height = 40.cgFloat
    
    var leftEdge = 8.cgFloat

    var space = 5.cgFloat
    
    var btnWidth = 46.cgFloat
    
    var tab_image_array = [String](){
        didSet{
            
            self.removeSubviews()
            var last_btn:ST_BTCustomEmojiSegmentItemView?
            for (idx,item) in tab_image_array.enumerated(){
                
                let itemBtn = ST_BTCustomEmojiSegmentItemView()

                itemBtn.coverUrl = item
                
                self.addSubview(itemBtn)
                
                itemBtn.snp.makeConstraints { (make) in
                    if let last = last_btn{
                        make.left.equalTo(last.snp.right).offset(space)
                    }else{
                        make.left.equalToSuperview().offset(leftEdge)
                    }
                    make.top.equalToSuperview().offset(5)
                    make.bottom.equalToSuperview().offset(-5)
                    make.width.equalTo(self.btnWidth)
                }
              
                
                itemBtn.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
                
                last_btn = itemBtn
                
                itemBtn.tag = baseTag + idx
                
                
                if idx == 0{
                    itemBtn.isSelected = true
                    last_selected_btn = itemBtn
                }
            }
        }
    }
    
    fileprivate var last_selected_btn:ST_BTCustomEmojiSegmentItemView?
    
    fileprivate let baseTag = 1237
    
    
    var selected_closure:((Int)->())?
}

//MARK:- Event
extension ST_BTCustomEmojiSegmentControl{
    
    func selectBtn(index:Int){
        
        let find = self.subviews.first { (v) -> Bool in
            return v.tag == self.baseTag + index
        }
        
        if let btn = find as? ST_BTCustomEmojiSegmentItemView{
            self.clickedLayout(sender: btn)
        }
    }
    
    fileprivate func clickedLayout(sender:ST_BTCustomEmojiSegmentItemView){
        
        if sender.isSelected{
            return
        }
        
        
        last_selected_btn?.isSelected = false
        sender.isSelected = true
        last_selected_btn = sender
        
    }
    
    @objc fileprivate func click_handle(sender:ST_BTCustomEmojiSegmentItemView){
        
        if sender.isSelected{
            return
        }
        self.clickedLayout(sender: sender)
        
        self.selected_closure?(sender.tag - self.baseTag)
        
    }
    
}



fileprivate class ST_BTCustomEmojiSegmentItemView:UIControl{
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.cornerRadius = 15
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 4, left: 12, bottom: 4, right: 12))
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var cover_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
      }()

    
    var coverUrl = ""{
        didSet{
            
            cover_image_view.pin_setImage(from: coverUrl.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        }
    }
    
    
    override var isSelected: Bool{
        didSet{
            
            
            if isSelected{
                self.backgroundColor = UIColor.init(hex: 0xDFE3E9)
            }else{
                self.backgroundColor = UIColor.clear
            }
            
        }
    }
    
}
